package controller;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import model.Prescription;
import model.PrescriptionBox;
import model.PrescriptionLabel;
import model.PrescriptionObject;
import view.Canvas;
import view.Designer;

/**
 * An event handler class for the mouse actions upon the canvas
 * @author Daniel Bond, Mohammed Abdullah, Gideon Pyzer
 *
 */
public class CanvasMouseListener extends MouseAdapter{	
	/**
	 * The actions to be performed upon the mouse being pressed
	 * @param e - the mouse event
	 */
	public void mousePressed(MouseEvent e){
		Canvas canvas = Canvas.getInstance();
		Prescription prescription = Prescription.getInstance();

	

		//Iterate through the prescription objects in reverse order
		for(int n = prescription.objects().size() - 1; n >=0; n--){
			PrescriptionObject object = prescription.objects().get(n);

			//For each object check if cursor was in it's bounds
			if(object.checkBounds(e.getX(), e.getY())){

				canvas.setStartCoordinates(e.getX(), e.getY());

				//Now check if it was in it's resizing square bounds
				if(object.checkResizeBounds(e.getX(), e.getY())){

					//Set the relevant booleans to true
					object.setResizing(true);
					canvas.setResize(true);

					canvas.setObjectDimensions(object.getX(),
							object.getY(),
							object.getWidth(),
							object.getHeight());

					canvas.setCurrentCoordinates(e.getX(), e.getY());
					return;
				}

				canvas.setResize(false);
				canvas.setSelected(true);
				canvas.setDrag(true);

				object.setDrag(true);

				prescription.setSelected(n);
				object.setSelected(true);

				canvas.setDraggedDimensions(object.getX(), object.getY(), object.getWidth(), object.getHeight());
				canvas.setCurrentCoordinates(object.getX(), object.getY());

				canvas.repaint();
				return;
			}
			else{
				object.setSelected(false);
				object.setHoverOver(false);
				object.setDrag(false);
				canvas.setSelected(false);
				canvas.setResize(false);
			}
			
		}
	}

	/**
	 * The actions to be performed upon the mouse being released
	 * @param e - the mouse event
	 */
	public void mouseReleased(MouseEvent e){
		Prescription prescription = Prescription.getInstance();
		Canvas canvas = Canvas.getInstance();
		Designer designer = Designer.getInstance();

		//for(PrescriptionObject object : pres1.objects()){
		for(int n = prescription.objects().size() - 1; n >=0; n--){
			PrescriptionObject object = prescription.objects().get(n);
			if(object.isDragging()){
			
				//object.setX(object.getX() + distanceX);
				//object.setY(object.getY() + distanceY);
				object.setX(canvas.getDragX());
				object.setY(canvas.getDragY());

			//	canvas.checkBoundaries(); <-- uncomment this for snapping functionality
				object.setDrag(false);
				canvas.setDrag(false);
				canvas.setSelected(true);
				
				canvas.repaint();
				return;
			}
			else if(canvas.isResize() && object.isSelected()){
				object.setX(canvas.getResizeX());
				object.setY(canvas.getResizeY());
				object.setWidth(canvas.getResizeWidth());
				object.setHeight(canvas.getResizeHeight());

				object.setSelected(true);
				object.setResizing(false);
				canvas.setResize(false);
				canvas.repaint();

				designer.update();
				return;
			}


		}
	}

	/**
	 * The actions to be performed upon the mouse being clicked
	 * @param e - the mouse event
	 */
	public void mouseClicked(MouseEvent e){
		Prescription prescription = Prescription.getInstance();
		Canvas canvas = Canvas.getInstance();
		Designer designer = Designer.getInstance();

		//Iterate through the object list
		for(int n = prescription.objects().size() - 1; n >=0; n--){
			PrescriptionObject object = prescription.objects().get(n);
			if(object.checkBounds(e.getX(), e.getY())){
				object.setSelected(true);
				canvas.setSelected(true);
				canvas.repaint();
				designer.update();

				//If the user has clicked the right button while on an object
				if(e.getButton() == MouseEvent.BUTTON3){
					if(object instanceof PrescriptionBox){
						if(object.isTransparent()){
							canvas.setMenuText("Opaque");
						}
					}else if(object instanceof PrescriptionLabel){
						if(object.isTransparent()){
							canvas.setMenuText("Opaque");
						}
					}else{
						canvas.setMenuText("Transparent");
					}

					//Display right click menu where user clicks
					canvas.showMenu(e.getXOnScreen(), e.getYOnScreen());
				}
				return;
			}
			else{
				canvas.setSelected(false);
				object.setSelected(false);
				canvas.repaint();
			}
		}
		//If the user has clicked the right button
		if(e.getButton() == MouseEvent.BUTTON3){
			canvas.showBGMenu(e.getXOnScreen(), e.getYOnScreen(), e.getX(), e.getY());
		}
		return;
	}
}